/**
 * Created with IntelliJ IDEA.
 * User: osereda
 * Date: 30.05.13
 */
package com.google.code.bankagregator.adapter.bank.sberrus.reader.credit;

import com.google.code.bankagregator.adapter.exception.AdapterException;
import com.google.code.bankagregator.common.SiteConnect;
import com.google.code.bankagregator.entities.Credit;
import com.google.code.bankagregator.entities.CreditType;
import com.google.code.bankagregator.entities.Currency;
import com.google.code.bankagregator.entities.State;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Per bank product group class
 * Return all data for products located in same "physical" source
 */
public class CreditSberVyhodnyi extends AbstractCreditSber {

    private final String SELECTOR_HTML = "div#content div.text table tbody tr";
    private final String PRODUCT_TITLE = "Кредит «Выгодный»";
    private final String CREDIT_SUM_SELECTOR = "Сумма кредита";
    private final String RATE_SELECTOR = "физических лиц";
    private final String TERM_SELECTOR = "Срок кредита";

    private final int DAYS_PER_MONTH = 31;
    private final int MONTHS_PER_YEAR = 12;

    /**
     * @param url string URL of specified product page
     */
    public CreditSberVyhodnyi(String url) {
        super(url);
    }

    /**
     * Get credit Vyhodnyi data
     *
     * @return
     * @throws AdapterException
     */
    @Override
    public List<Credit> getCredits() throws AdapterException {
        List<Credit> credits = new ArrayList<>();
        Credit credit = new Credit();

        Document page = new SiteConnect(this.getCreditPageURL()).getPage();
        Elements elements = page.select(SELECTOR_HTML);

        credit.setType(CreditType.CONSUMER);
        credit.setPrepayment(0.0);
        credit.setBank(this.getBank());
        credit.setCurrency(Currency.UAH);
        credit.setName(PRODUCT_TITLE);
        credit.setUrl(this.getCreditPageURL());
        for (Element elm : elements) {
            Elements el = elm.children();
            String cellTitle = el.get(0).text();
            String cellValue = el.get(1).text();

            if (cellTitle.equals(CREDIT_SUM_SELECTOR)) {
                String[] amounts = getAmountFromLine(cellValue);
                credit.setMaxAmount(Integer.valueOf(amounts[1]));
                credit.setMinAmount(Integer.valueOf(amounts[0]));
            }

            if (cellTitle.contains(RATE_SELECTOR)) {
                String rate = cellValue.split("%")[0].trim();
                credit.setCreditRate(Double.valueOf(rate));
            }

            if (cellTitle.equals(TERM_SELECTOR)) {
                String term = cellValue.split(UNTIL_SELECTOR)[1].replaceAll("[^0-9]", "").trim();
                credit.setTerm(MONTHS_PER_YEAR * DAYS_PER_MONTH * Integer.valueOf(term));
            }
            credit.setState(State.ACTIVE);
        }

        credits.add(credit);
        return credits;
    }

}
